//
//  BBDMessageTableViewCell.m
//  棒棒哒
//
//  Created by rimi on 15/8/4.
//  Copyright (c) 2015年 rimi. All rights reserved.
//

#import "BBDMessageTableViewCell.h"
#import "CustomLabel.h"
#import "Config.h"

@interface BBDMessageTableViewCell ()


@property (nonatomic, strong) NSDateFormatter *formatter;

@end

@implementation BBDMessageTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initializeUserInterFace];
    }
    return self;
}

- (void)initializeUserInterFace {
    _messageView = [[UITextView alloc] initWithFrame:CGRectZero];
    _messageView.font = [UIFont systemFontOfSize:16];
    _messageView.backgroundColor = [UIColor whiteColor];
    _messageView.scrollEnabled = NO;
    _messageView.editable = NO;
    [self.contentView addSubview:_messageView];
    
    _headImage = [[UIImageView alloc] initWithFrame:CGRectZero];
    _headImage.backgroundColor = [UIColor clearColor];
//    [_headImage sizeToFit];
    [self.contentView addSubview:_headImage];
    
    _timeLabel = [CustomLabel labelWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15) Text:nil FontSize:12 TextAlignment:NSTextAlignmentLeft TextColor:[UIColor redColor] BackGroundColor:[UIColor whiteColor] CornerRadius:0 BorderWidth:0 BorderColor:nil];
    _timeLabel.backgroundColor = [UIColor whiteColor];
    _timeLabel.textAlignment = NSTextAlignmentCenter;
    _timeLabel.center = CGPointMake(SCREEN_WIDTH / 2, 5);
    [self.contentView addSubview:_timeLabel];
    
    _backImage = [_backImage stretchableImageWithLeftCapWidth:20 topCapHeight:20];
}

- (void)setMessage:(NSString *)message {
    CGRect contentRect = [message boundingRectWithSize:CGSizeMake(self.bounds.size.width * 2 / 3, DBL_MAX) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16]} context:nil];
    if (_type == MessageTypeMe) {
        _messageView.frame = CGRectMake(SCREEN_WIDTH - contentRect.size.width - 40, 25, contentRect.size.width + 30, contentRect.size.height + 20);
        _headImage.frame = CGRectZero;
    } else {
        _messageView.frame = CGRectMake(60, 25, contentRect.size.width + 20, contentRect.size.height + 20);
        _headImage.frame = CGRectMake(10, 25, 50, 50);
         _headImage.image = [UIImage imageNamed:@"123.png"];
        
    }
    _messageView.text = message;
}

- (void)setType:(MessageType)type {
    if (type == MessageTypeMe) {
        _backImage = [UIImage imageNamed:@"chatto_bg_normal副本"];
    } else {
        _backImage = [UIImage imageNamed:@"chatfrom_bg_normal副本"];
    }
    _type = type;
    _messageView.layer.contents = (__bridge id)(_backImage.CGImage);
}

@end
